﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SsTool.Common;
using SmartSpace.Data;

namespace SsTool.Metadata.Windows
{
    /// <summary>
    /// Interaction logic for CreatePropertyWindow.xaml
    /// </summary>
    public partial class CreatePropertyWindow : Window
    {
        private EntityMetadata _entity;
        public SmartSpace.Data.PropertyMetadata PropertyMetadata { get; private set; }

        public CreatePropertyWindow(EntityMetadata entityMetadata)
        {
            InitializeComponent();

            _entity = entityMetadata;
            PropertyMetadata = new SmartSpace.Data.PropertyMetadata();
            PropertyMetadata.Id = Guid.NewGuid();
            PropertyMetadata.EntityId = entityMetadata.Id;
            PropertyMetadata.IsNullable = true;
            this.DataContext = PropertyMetadata;

            if (_entity.Properties == null)
            {
                _entity.Properties = new List<SmartSpace.Data.PropertyMetadata>();
            }
            _entity.Properties.Add(PropertyMetadata);
        }

        public CreatePropertyWindow(EntityMetadata entityMetadata, SmartSpace.Data.PropertyMetadata property)
        {
            InitializeComponent();

            _entity = entityMetadata;
            foreach (var item in _entity.Properties)
            {
                if (item.Id == property.Id)
                {
                    PropertyMetadata = item;
                }
            }
            this.DataContext = PropertyMetadata;
        }

        private void _ctlBtnSubmit_Click(object sender, RoutedEventArgs e)
        {
            IDataService service = App.Get<IDataService>();
            if (service.StoreEntity(_entity))
            {
                DialogResult = true;
            }
        }

        private void _ctlBtnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void _ctlBtnSelectType_Click(object sender, RoutedEventArgs e)
        {
            SelectPropertyTypeWindow window = new SelectPropertyTypeWindow();
            if (window.ShowDialog() == true)
            {
                PropertyMetadata.Type = window.SelectedType;
                PropertyMetadata.TypeId = window.SelectedType.Id;
                PropertyMetadata.MinLength = window.SelectedType.MinLength;
                PropertyMetadata.MaxLength = window.SelectedType.MaxLength;

                this.DataContext = null;
                this.DataContext = PropertyMetadata;
            }
        }

        private void _ctlBtnSelectTypeFlag_Click(object sender, RoutedEventArgs e)
        {
            if (this.PropertyMetadata.Type == null)
            {
                MessageBox.Show("请先选择类型!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (this.PropertyMetadata.Type.Name == "dictionary")
            {
                SelectDictionaryTypeWindow window = new SelectDictionaryTypeWindow();
                if (window.ShowDialog() == true)
                {
                    this.PropertyMetadata.TypeFlag = window.SelectedDictionaryType.Id.ToString();
                    this.PropertyMetadata.TypeFlagCaption = window.SelectedDictionaryType.Caption;
                    this.DataContext = null;
                    this.DataContext = PropertyMetadata;
                }
            }
        }
    }
}
